package com.Khan.SocialRescueSys.controller;

import com.Khan.SocialRescueSys.pojo.Force;
import com.Khan.SocialRescueSys.pojo.Province;
import com.Khan.SocialRescueSys.pojo.Type;
import com.Khan.SocialRescueSys.service.ForceService;
import com.Khan.SocialRescueSys.service.ProvinceService;
import com.Khan.SocialRescueSys.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Khan Liu
 * @date 2022/4/4 21:14
 */
@Controller
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private ForceService forceService;

    @GetMapping("/provinces/{id}")
    public String types(@PathVariable Long id, @RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                        Model model){
        PageHelper.startPage(pagenum, 10);  //开启分页
        List<Province> provinces = provinceService.getForceProvince();
        //-1从导航点过来的
        if (id == -1){
            id = provinces.get(0).getId();
        }
        List<Force> forces = forceService.getByProvinceId(id);
        PageInfo<Force> pageInfo = new PageInfo<>(forces);
        model.addAttribute("provinces", provinces);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("activeProvinceId", id);

        return "provinces";
    }
}
